package com.wk.lib_base.net

import okhttp3.Interceptor
import okhttp3.Response

class RequestHeaderInterceptor : Interceptor {

    val headersMap = mapOf<String, String>(
        "charset" to "UTF-8"
    )

    override fun intercept(chain: Interceptor.Chain): Response {
        val requestOrigin = chain.request()
        val url = requestOrigin.url
        if (headersMap.isNotEmpty()) {
            val requestWithHeaderBuilder = requestOrigin.newBuilder()
            headersMap.entries.forEach {
                requestWithHeaderBuilder.addHeader(it.key, it.value)
            }
            val newResponse = requestWithHeaderBuilder.url(url).build()
            return chain.proceed(newResponse)
        }
        return chain.proceed(requestOrigin)
    }

}